package org.bozo.im.service.protocols.xmpp.session;

import java.io.IOException;
import java.net.Socket;

import org.bozo.im.service.Constants;
import org.bozo.im.service.protocols.xmpp.XMPPSession;
import org.bozo.im.service.protocols.xmpp.stanza.Stanza;
import org.bozo.im.service.protocols.xmpp.stanza.processor.StanzaProcessor;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Log;


public class XMPPReader {

	// ---------------------------------------------------------------------------------------
	// Variables
	// ---------------------------------------------------------------------------------------
	
	private XMPPSession mSession;
	
	private Thread mThread;
	private XmlPullParser mParser;
	
	public XMPPReader(XMPPSession pSession, Socket pSocket) throws XmlPullParserException, IOException {
		mSession = pSession;

		updateSocket(pSocket);
	}
	
	public void startListener() {
		mThread = new Thread(new Runnable() {
			
			public void run() {
				listen();
			}
		});

		mThread.start();
	}
	
	public void join() throws InterruptedException {
		mThread.join();
	}
	
	public void updateSocket(Socket pSocket) throws IOException, XmlPullParserException {

		XmlPullParserFactory lParserFactory = XmlPullParserFactory.newInstance();
		lParserFactory.setNamespaceAware(true);
		mParser = lParserFactory.newPullParser();
		mParser.setInput(pSocket.getInputStream(), "UTF-8");
	}
	
	private void listen() {
		try {

			// ---------------------------------------------------------------------------------------
			Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.xmpp.session.XMPPReader.listen ---------- Initiating XML Pull Parser");
			// ---------------------------------------------------------------------------------------
			
			while (mParser.getEventType() != XmlPullParser.END_DOCUMENT) {
				
				if (mParser.getEventType() == XmlPullParser.START_TAG) {

					Stanza lStanza = Stanza.valueOf(mParser.getName(), mParser.getNamespace());

					// ---------------------------------------------------------------------------------------
					Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.xmpp.session.XMPPReader.listen ---------- Parsing XML node: " + lStanza);
					// ---------------------------------------------------------------------------------------
					
					StanzaProcessor lStanzaProcessor = mSession.getStanza(lStanza);
					
					if (lStanzaProcessor != null) {
						lStanzaProcessor.processStanza(mParser, mSession, null);
					} else {
						// ---------------------------------------------------------------------------------------
						Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.xmpp.session.XMPPReader.listen ---------- Unknown stanza : " + mParser.getName());
						// ---------------------------------------------------------------------------------------
					}

					mSession.getTaskProcessor().processTasks();
				}
				
				mParser.next();
			}

			// ---------------------------------------------------------------------------------------
			Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.xmpp.session.XMPPReader.listen ---------- Leaving the listening thread");
			// ---------------------------------------------------------------------------------------
		} catch (IOException e) {
			// ---------------------------------------------------------------------------------------
			Log.e(Constants.ACTIVITY_TAG, "org.bozo.im.xmpp.session.XMPPReader.listen ---------- Unable to retrieve the connection from the server", e);
			// ---------------------------------------------------------------------------------------
		} catch (XmlPullParserException e) {
			// ---------------------------------------------------------------------------------------
			Log.e(Constants.ACTIVITY_TAG, "org.bozo.im.xmpp.session.XMPPReader.listen ---------- Error while parsing the XML data", e);
			// ---------------------------------------------------------------------------------------
		}
	}
}
